package com.lanxin.oa.approve.service;

import com.lanxin.bean.LcFlow;
import com.lanxin.oa.approve.dao.ApproveDao;
import com.lanxin.oa.approve.dao.NodeDao;
import com.lanxin.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApproveServiceImpl implements ApproveService {

    @Autowired
    private ApproveDao approveDao;

    @Autowired
    private NodeDao nodeDao;

    @Override
    public PageBean<LcFlow> getApprovePage(Integer page, Integer pageSize, LcFlow flow, String docType1) {
        return approveDao.selectApprovePage(page, pageSize, flow, docType1);
    }

    @Override
    public boolean updateApproveStatus(Integer oid, String state) {
        if ("Y".equals(state)) {
            List listNode = nodeDao.selectById(oid);
            if (listNode == null || listNode.size() == 0)
                return false;
        }
        LcFlow flow = approveDao.findFlowInfo(oid);
        flow.setState(state);
        approveDao.update(flow);
        return true;
    }

    @Override
    public boolean selectApproveByName(String flowName, Integer oid) {
        LcFlow flow = approveDao.selectLcFlowByName(flowName, oid);
        if (flow != null) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteApprove(Integer oid) {
        try {
            List listNode = nodeDao.selectById(oid);
            if (listNode != null && listNode.size() > 0)
                return false;
            LcFlow flow = approveDao.findFlowInfo(oid);
            approveDao.delete(flow);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void editApprove(LcFlow flow) {
        approveDao.saveOrUpdate(flow);
    }
}
